//
//  ToMailController.m
//  InTheSnow
//
//  Created by di on 7/31/09.
//  Copyright 2009 INTELLECTSOFT. All rights reserved.
//
#import "News.h"
#import "ToMailController.h"


@implementation ToMailController

@synthesize currentMailNew;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil news:(News*)n;{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		self.currentMailNew = n;
    }
    return self;
}

#pragma mark -
#pragma mark Compose Mail

// Displays an email composition interface inside the application. Populates all the Mail fields. 
-(void)displayComposerSheet 
{
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	
	News *n = currentMailNew;//[[__delegate rssContentEvents] objectAtIndex:newsNum];
	[picker setSubject:n.title];
	
	
	// Set up recipients
	NSArray *toRecipients = [NSArray array]; 
	NSArray *ccRecipients = [NSArray array]; 
	NSArray *bccRecipients = [NSArray array]; 
	
	[picker setToRecipients:toRecipients];
	[picker setCcRecipients:ccRecipients];	
	[picker setBccRecipients:bccRecipients];
	
	// Attach an image to the email
	 
    NSData *myData = [NSData dataWithContentsOfURL:n.thumbImage];
	if(myData !=nil)
		[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@""];
	
	// Fill out the email body text
	NSString *emailBody = n.description;
	[picker setMessageBody:emailBody isHTML:NO];
	
	[self presentModalViewController:picker animated:YES];
    [picker release];
}


// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{	
	 	[self dismissModalViewControllerAnimated:YES];
	[[__delegate navigationController] popViewControllerAnimated:YES];
}


#pragma mark -
#pragma mark Workaround

// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
	NSString *recipients = @"mailto:subject=Hello from California!";
	NSString *body = @"From InTheSnow";
	
	NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
	email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}


 
 
 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
	if (mailClass != nil)
	{
		// We must always check whether the current device is configured for sending emails
		if ([mailClass canSendMail])
		{
			[self displayComposerSheet];
		}
		else
		{
			[self launchMailAppOnDevice];
		}
	}
	else
	{
		[self launchMailAppOnDevice];
	}
	
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
